package cn.nawang.ebeim.api.repository.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.nawang.ebeim.api.comm.BaseQuery;
import cn.nawang.ebeim.api.comm.PageList;
import cn.nawang.ebeim.api.comm.Pages;
import cn.nawang.ebeim.api.domain.TbStorageStrategy;
import cn.nawang.ebeim.api.utils.ConvertUtil;

/**
 * @ClassName: TbStorageStrategyRepositoryImpl
 * @Description: TODO
 * @author fanyb
 * @date Oct 12, 2015 11:37:13 AM
 * @version v1.0
 */
public class TbStorageStrategyRepositoryImpl extends BaseQuery{

  public List<TbStorageStrategy> findByCustomerId(String customerId) {
    String sql = "select new cn.nawang.ebeim.api.domain.TbStorageStrategy"
        + "(ss.id,ss.customerId,ss.storageCloudId,ss.isPrimary,c.name,sc.name) "
        + "from TbStorageStrategy ss,Customer c,StorageCloud sc where ss.customerId = :customerId "
        + "and ss.customerId=c.id and sc.id=ss.storageCloudId";
    Map<String,Object> paraMap = new HashMap<String,Object>();
    try{
      paraMap.put("customerId", customerId);
      List<TbStorageStrategy> tb_StorageStrategy = queryObject(sql,paraMap);
      if(tb_StorageStrategy!=null && !tb_StorageStrategy.isEmpty()){
        return tb_StorageStrategy;
      }
    }catch(Exception ex){
      ex.printStackTrace();
    }
    return null;
    
  }
  
  /**
   * 
   * @author fanyb
   * @Description: 根据云存储编号查找存储策略对象
   * @param storageCloudId
   * @return TbStorageStrategy 
   * @date Oct 12, 201511:58:22 AM
   *
   * @修改人 
   * @修改日期
   */
  public TbStorageStrategy findByStorageCloudId(String storageCloudId){
    String sql="select new cn.nawang.ebeim.api.domain.TbStorageStrategy"
        + "(ss.id,ss.customerId,ss.storageCloudId,ss.isPrimary,c.name,sc.name) "
        + "from TbStorageStrategy ss,Customer c,StorageCloud sc where ss.storageCloudId = :storageCloudId "
        + "and ss.customerId=c.id and sc.id=ss.storageCloudId";
    Map<String,Object> paraMap = new HashMap<String,Object>();
    try{
      paraMap.put("storageCloudId", storageCloudId);
      List<TbStorageStrategy> tbStorageStrategys = queryObject(sql,paraMap);
      if(tbStorageStrategys!=null && !tbStorageStrategys.isEmpty()){
        return tbStorageStrategys.get(0);
      }
    }catch(Exception ex){
      ex.printStackTrace();
    }
    return null;
  }
  
  public PageList findAllByPaging(Pages page) {
    String hqlCount = "select count(tss.id) from TbStorageStrategy tss,Customer c,StorageCloud sc where "
        + " tss.customerId=c.id and sc.id=tss.storageCloudId and 1=1 ";
    String hqlList="select new cn.nawang.ebeim.api.domain.TbStorageStrategy"
        + "(tss.id,tss.customerId,tss.storageCloudId,tss.isPrimary,c.name,sc.name) "
        + "from TbStorageStrategy tss,Customer c,StorageCloud sc where "
        + " tss.customerId=c.id and sc.id=tss.storageCloudId and 1=1 ";
    String order=" order by tss.id desc";
    PageList pl = null;
    try{
      String cod = joinSearchCondition(page);//拼接查询条件
      pl = queryListByPaging(hqlCount+cod+order, hqlList+cod+order, page);
    }catch (Exception ex) {
      ex.printStackTrace();
    }
    return pl;
  }
  
  public boolean delete(String[] ids){
    String values=ConvertUtil.toDbString(ids);
    String hqlList="delete from TbStorageStrategy where id in"+values;
    Map<String, Object> paraMap = new HashMap<String, Object>();
    try{
      boolean deleteFlag = this.execHQL(hqlList, paraMap);
      return deleteFlag;
    }catch(Exception e){
      e.printStackTrace();
    }
    return false;
  }
  
  public TbStorageStrategy findByIsPrimary(String customerId,int isPrimary){
    String hql="from TbStorageStrategy where customerId=:customerId and isPrimary=:isPrimary";
    Map<String, Object> paraMap = new HashMap<String, Object>();
    try{
      paraMap.put("customerId",customerId);
      paraMap.put("isPrimary", isPrimary);
      List<TbStorageStrategy> tbStorageStrategys = queryObject(hql,paraMap);
      if(tbStorageStrategys!=null && !tbStorageStrategys.isEmpty()){
        return tbStorageStrategys.get(0);
      }
    }catch(Exception e){
      e.printStackTrace();
    }
    return null;
  }
  
  public TbStorageStrategy findByStorageCloudIdAndCustomerId(String customerId,String storageCloudId){
    String hql="from TbStorageStrategy where customerId=:customerId and storageCloudId=:storageCloudId";
    Map<String, Object> paraMap = new HashMap<String, Object>();
    try{
      paraMap.put("customerId",customerId);
      paraMap.put("storageCloudId", storageCloudId);
      List<TbStorageStrategy> tbStorageStrategys = queryObject(hql,paraMap);
      if(tbStorageStrategys!=null && !tbStorageStrategys.isEmpty()){
        return tbStorageStrategys.get(0);
      }
    }catch(Exception e){
      e.printStackTrace();
    }
    return null;
  }
}
